package com.danielagarza.app;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;

import com.danielagarza.app.R;
import com.danielagarza.app.modelos.Evento;
import com.danielagarza.app.modelos.Tweet;
import com.danielagarza.app.servicios.LeerJSON;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Twitter extends Fragment{
	
	
	
	
	public Twitter() {
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		View vista = inflater.inflate(R.layout.activity_twitter, container, false);
		ListView lv = (ListView)vista.findViewById(R.id.listView1);
		CargarDatos t = new CargarDatos();
		t.execute();
		try {
			Gson g = new Gson();
			List<Tweet> l = g.fromJson(t.get(), new TypeToken<List<Tweet>>(){}.getType());
			ItemCompraAdapter adapter = new ItemCompraAdapter(this.getActivity(),l);
			lv.setAdapter(adapter);
			
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return vista;
	}
	

	private class CargarDatos extends AsyncTask<Void, Integer, String> {
		@Override
		protected String doInBackground(Void... arg0) {
			String valor = LeerJSON.leerJSON("http://api.twitter.com/1/statuses/user_timeline/DanielaGarzaM.json?count=15");
			return valor;
		}
	}
	
	public class ItemCompraAdapter extends BaseAdapter {
		  protected Activity activity;
		  protected List<Tweet> items;
		         
		  public ItemCompraAdapter(Activity activity, List<Tweet> items) {
		    this.activity = activity;
		    this.items = items;
		  }
		 
		  @Override
		  public int getCount() {
		    return items.size();
		  }
		 
		  @Override
		  public Object getItem(int position) {
		    return items.get(position);
		  }
		 
		  @Override
		  public View getView(int position, View convertView, ViewGroup parent) {
		    View vi=convertView;
		         
		    if(convertView == null) {
		      LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		      vi = inflater.inflate(R.layout.tweet_layout, null);
		    }
		             
		    Tweet item = items.get(position);
		         
		    ImageView image = (ImageView) vi.findViewById(R.id.imageView1);
		  //  int imageResource = activity.getResources().getIdentifier(item.getRutaImagen(), null, activity.getPackageName());
		  //  image.setImageDrawable(activity.getResources().getDrawable(imageResource));
		         
		    TextView nombre = (TextView) vi.findViewById(R.id.textView1);
		    nombre.setText(item.created_at);
		         
		    TextView tipo = (TextView) vi.findViewById(R.id.textView2);
		    tipo.setText(item.text);
		 
		    return vi;
		  }

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}
		}
}
